﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace orders
{
    class MotabaaEntity
    {
        public Int32 Id { get; set; }
        public string Today { get; set; }
        public DateTime Dates { get; set; }
        public string NameOfBox { get; set; }
        public string hieght { get; set; }
        public string width { get; set; }
        public string CountOfPaper { get; set; }
        public Boolean OrderOfPrint { get; set; }
        public Boolean ServiceAfterPrint { get; set; }
        public Boolean Cartoon { get; set; }
        public string Notes { get; set; }
        public string Name_Estamba { get; set; }
        public DateTime Date_Cartoon { get; set; }

    }

    class Motabaa
    {
        private static String connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='motabaa.accdb' ;Jet OLEDB:Database Password=Yarabe11";
        private static String cmdText = "";

        public static bool insert(MotabaaEntity m)
        {
            bool flag = false;
            cmdText = "INSERT INTO motabaa ( Today,Dates,NameOfBox, hieght ,width, CountOfPaper ,OrderOfPrint, ServiceAfterPrint,Cartoon, Notes , Name_Estamba ,Date_Cartoon) values (?,?,?,?,?,?,?,?,?,?,?,?)";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@Today", m.Today);
            cmd.Parameters.AddWithValue("@Dates", m.Dates.ToShortDateString());
            cmd.Parameters.AddWithValue("@NameOfBox", m.NameOfBox);
            cmd.Parameters.AddWithValue("@hieght", m.hieght);
            cmd.Parameters.AddWithValue("@width", m.width);
            cmd.Parameters.AddWithValue("@CountOfPaper", m.CountOfPaper);
            cmd.Parameters.AddWithValue("@OrderOfPrint", m.OrderOfPrint);
            cmd.Parameters.AddWithValue("@ServiceAfterPrint", m.ServiceAfterPrint);
            cmd.Parameters.AddWithValue("@Cartoon", m.Cartoon);
            cmd.Parameters.AddWithValue("@Notes", m.Notes);
            cmd.Parameters.AddWithValue("@Name_Estamba", m.Name_Estamba);
            if (m.Date_Cartoon > DateTime.Parse("1/1/2001"))
            {
                cmd.Parameters.AddWithValue("@Date_Cartoon", m.Date_Cartoon.ToShortDateString());
            }
            else
            {
                cmd.Parameters.AddWithValue("@Date_Cartoon", DBNull.Value);
            }
            
            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Insert" + e.Message);
            }
            finally
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;

        }
        public static bool Update(MotabaaEntity m)
        {
            bool flag = false;
            cmdText = "Update motabaa set Today= @Today , Dates = @Dates ,NameOfBox=@NameOfBox, hieght =@hieght, width=@width , CountOfPaper =@CountOfPaper , OrderOfPrint=@OrderOfPrint ,ServiceAfterPrint=@ServiceAfterPrint, Cartoon=@Cartoon, Notes=@Notes,Name_Estamba=@Name_Estamba,Date_Cartoon=@Date_Cartoon Where Id = @Id";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Today", m.Today);
            cmd.Parameters.AddWithValue("@Dates", m.Dates.ToShortDateString());
            cmd.Parameters.AddWithValue("@NameOfBox", m.NameOfBox);
            cmd.Parameters.AddWithValue("@hieght", m.hieght);
            cmd.Parameters.AddWithValue("@width", m.width);
            cmd.Parameters.AddWithValue("@CountOfPaper", m.CountOfPaper);
            cmd.Parameters.AddWithValue("@OrderOfPrint", m.OrderOfPrint);
            cmd.Parameters.AddWithValue("@ServiceAfterPrint", m.ServiceAfterPrint);
            cmd.Parameters.AddWithValue("@Cartoon", m.Cartoon);
            cmd.Parameters.AddWithValue("@Notes", m.Notes);
            cmd.Parameters.AddWithValue("@Name_Estamba", m.Name_Estamba);
            cmd.Parameters.AddWithValue("@Date_Cartoon", m.Date_Cartoon);
            cmd.Parameters.AddWithValue("@Id", m.Id);

            try
            {
                con.Open();
                Int32 x = cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error In Update" + e.Message);
            }
            finally
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            return flag;


        }
        public static bool delete(int id)
        {
            bool flag = true;
            cmdText = "delete from motabaa where Id = ? ";
            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@Id", id);

            try
            {
                con.Open();
                int x = cmd.ExecuteNonQuery();
                if (x == 0)
                {
                    flag = false;

                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in Delet" + e.Message);

            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

            }
            return flag;

        }
        public static MotabaaEntity GetById(int x)
        {
            MotabaaEntity t = null;
            cmdText = "Select * from motabaa where Id=?";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@Id", x);
            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetMotabaaId ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return t;
        }
        public static List<MotabaaEntity> GetAll()
        {
            List<MotabaaEntity> listEntity = new List<MotabaaEntity>();
            MotabaaEntity t = null;
            cmdText = "Select * from motabaa";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    listEntity.Add(t);

                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetMotabaaId ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return listEntity;

        }
        public static List<MotabaaEntity> GetByNamebox(String Name_box)
        {
            List<MotabaaEntity> listEntity = new List<MotabaaEntity>();
            MotabaaEntity t = null;
            cmdText = "Select * from motabaa where NameOfBox Like ?  ORDER BY Dates DESC";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Name_box", Name_box);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    listEntity.Add(t);

                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetByNamebox ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return listEntity;




        }
        public static MotabaaEntity fillEntityFromReader(OleDbDataReader reader)
        {
            MotabaaEntity t = new MotabaaEntity();
            if (reader["Id"] != null)
                t.Id = Int32.Parse(reader["Id"].ToString());
            if (reader["Today"] != null)  
                t.Today = reader["Today"].ToString();
            if (reader["NameOfBox"] != null)
                t.NameOfBox = reader["NameOfBox"].ToString();
            if (reader["Dates"] != null&&!string.IsNullOrEmpty(reader["Dates"].ToString()))
                t.Dates = DateTime.Parse(reader["Dates"].ToString());
            if (reader["hieght"] != null)
                t.hieght = reader["hieght"].ToString();
            if (reader["width"] != null)
                t.width = reader["width"].ToString();
            if (reader["CountOfPaper"] != null)
                t.CountOfPaper = reader["CountOfPaper"].ToString();
            if (reader["ServiceAfterPrint"] != null)
                t.ServiceAfterPrint = Convert.ToBoolean(reader["ServiceAfterPrint"].ToString());
            if (reader["OrderOfPrint"] != null)
                t.OrderOfPrint = Convert.ToBoolean(reader["OrderOfPrint"].ToString());
            if (reader["Cartoon"] != null)
                t.Cartoon = Convert.ToBoolean(reader["Cartoon"].ToString());
            if (reader["Notes"] != null)
                t.Notes = reader["Notes"].ToString();
            if (reader["Name_Estamba"] != null)
                t.Name_Estamba = reader["Name_Estamba"].ToString();
            if (reader["Date_Cartoon"] != null && !string.IsNullOrEmpty(reader["Date_Cartoon"].ToString()) && DateTime.Parse(reader["Date_Cartoon"].ToString())> DateTime.Parse("1/1/2001"))
            {
                t.Date_Cartoon = DateTime.Parse(reader["Date_Cartoon"].ToString());
            }

            return t;

        }

        public static List<MotabaaEntity> GetByNotes(String Notes)
        {
            List<MotabaaEntity> listEntity = new List<MotabaaEntity>();
            MotabaaEntity t = null;
            cmdText = "Select * from motabaa where Notes Like ?  ORDER BY Dates DESC";

            OleDbConnection con = new OleDbConnection(connstring);
            OleDbCommand cmd = new OleDbCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Notes", Notes);

            try
            {
                con.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    t = fillEntityFromReader(reader);
                    listEntity.Add(t);

                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetByNamebox ", ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return listEntity;
        }
    }
}